Thread: undefined reference to `sqrt' when using math.h

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    undefined reference to `sqrt' when using math.h

    Just like the title says..

    I get
    undefined reference to `sqrt'
    error and i included math.h


    Using GCC by the way, in linux.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    "undefined reference" is a linker error - meaning that you didn't link with the code for the function it's complaining about. (Most of the times, #including something will not fix this)

    For sqrt(), add -lm to your linker flags.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Cactus_Hugger View Post
    "undefined reference" is a linker error - meaning that you didn't link with the code for the function it's complaining about. (Most of the times, #including something will not fix this)

    For sqrt(), add -lm to your linker flags.
    What is a linker flags?

    I'm using gcc how should i do it?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Example:
    Code:
    $ gcc -c -o main.o main.c
    $ gcc -c -o second.o second.c
    $ gcc -o myprogram main.o second.o -lm
      or, if you're doing it all in one line:
    $ gcc -o myprogram main.c second.c -lm
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM